package helper;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

public class HttpHelper {
	public static HttpResponse executePost(String uri) {
		HttpResponse resp = null;
		
		try {
			HttpPost post = new HttpPost(uri);
			
			resp = new DefaultHttpClient().execute(post);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return resp;
	}
	
	public static HttpResponse executePost(String uri, String jsonStr) {
		HttpResponse resp = null;
		
		try {
			HttpPost post = new HttpPost(uri);
			List<NameValuePair> nameValPairList = new ArrayList<NameValuePair>();
			
			nameValPairList.add(new BasicNameValuePair("json", jsonStr));
			post.setEntity(new UrlEncodedFormEntity(nameValPairList, HTTP.UTF_8));
			resp = new DefaultHttpClient().execute(post);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return resp;
	}
}
